package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclzdgmdBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclzdgmdBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 最大干密度\最佳含水率试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-30
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclzdgmdBsInfoServiceImpl implements IWdclzdgmdBsInfoService {

    private final WdclzdgmdBsInfoMapper baseMapper;
    private final WdclzdgmdBaDetailMapper detailMapper;

    /**
     * 查询最大干密度\最佳含水率试验检测记录
     */
    @Override
    @FIllBasicInfo
    public WdclzdgmdBsInfoVo queryById(Long excelId){
        WdclzdgmdBsInfoVo wdclzdgmdBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdclzdgmdBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdclzdgmdBaDetail>()
            .eq(WdclzdgmdBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclzdgmdBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return wdclzdgmdBsInfoVo;
    }

    /**
     * 查询最大干密度\最佳含水率试验检测记录列表
     */
    @Override
    public TableDataInfo<WdclzdgmdBsInfoVo> queryPageList(WdclzdgmdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclzdgmdBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclzdgmdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询最大干密度\最佳含水率试验检测记录列表
     */
    @Override
    public List<WdclzdgmdBsInfoVo> queryList(WdclzdgmdBsInfoBo bo) {
        LambdaQueryWrapper<WdclzdgmdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclzdgmdBsInfo> buildQueryWrapper(WdclzdgmdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclzdgmdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclzdgmdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclzdgmdBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), WdclzdgmdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclzdgmdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclzdgmdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclzdgmdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclzdgmdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclzdgmdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclzdgmdBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclzdgmdBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclzdgmdBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), WdclzdgmdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclzdgmdBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclzdgmdBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclzdgmdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclzdgmdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclzdgmdBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclzdgmdBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclzdgmdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclzdgmdBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclzdgmdBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg1()), WdclzdgmdBsInfo::getCailmcgg1, bo.getCailmcgg1());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg2()), WdclzdgmdBsInfo::getCailmcgg2, bo.getCailmcgg2());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg3()), WdclzdgmdBsInfo::getCailmcgg3, bo.getCailmcgg3());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg4()), WdclzdgmdBsInfo::getCailmcgg4, bo.getCailmcgg4());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg5()), WdclzdgmdBsInfo::getCailmcgg5, bo.getCailmcgg5());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg6()), WdclzdgmdBsInfo::getCailmcgg6, bo.getCailmcgg6());
        lqw.eq(StringUtils.isNotBlank(bo.getCailmcgg7()), WdclzdgmdBsInfo::getCailmcgg7, bo.getCailmcgg7());
        lqw.eq(StringUtils.isNotBlank(bo.getChand1()), WdclzdgmdBsInfo::getChand1, bo.getChand1());
        lqw.eq(StringUtils.isNotBlank(bo.getChand2()), WdclzdgmdBsInfo::getChand2, bo.getChand2());
        lqw.eq(StringUtils.isNotBlank(bo.getChand3()), WdclzdgmdBsInfo::getChand3, bo.getChand3());
        lqw.eq(StringUtils.isNotBlank(bo.getChand4()), WdclzdgmdBsInfo::getChand4, bo.getChand4());
        lqw.eq(StringUtils.isNotBlank(bo.getChand5()), WdclzdgmdBsInfo::getChand5, bo.getChand5());
        lqw.eq(StringUtils.isNotBlank(bo.getChand6()), WdclzdgmdBsInfo::getChand6, bo.getChand6());
        lqw.eq(StringUtils.isNotBlank(bo.getChand7()), WdclzdgmdBsInfo::getChand7, bo.getChand7());
        lqw.eq(StringUtils.isNotBlank(bo.getBil1()), WdclzdgmdBsInfo::getBil1, bo.getBil1());
        lqw.eq(StringUtils.isNotBlank(bo.getBil2()), WdclzdgmdBsInfo::getBil2, bo.getBil2());
        lqw.eq(StringUtils.isNotBlank(bo.getBil3()), WdclzdgmdBsInfo::getBil3, bo.getBil3());
        lqw.eq(StringUtils.isNotBlank(bo.getBil4()), WdclzdgmdBsInfo::getBil4, bo.getBil4());
        lqw.eq(StringUtils.isNotBlank(bo.getBil5()), WdclzdgmdBsInfo::getBil5, bo.getBil5());
        lqw.eq(StringUtils.isNotBlank(bo.getBil6()), WdclzdgmdBsInfo::getBil6, bo.getBil6());
        lqw.eq(StringUtils.isNotBlank(bo.getBil7()), WdclzdgmdBsInfo::getBil7, bo.getBil7());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyfflb()), WdclzdgmdBsInfo::getShiyfflb, bo.getShiyfflb());
        lqw.eq(bo.getTongrj() != null, WdclzdgmdBsInfo::getTongrj, bo.getTongrj());
        lqw.eq(bo.getKlhl538mm() != null, WdclzdgmdBsInfo::getKlhl538mm, bo.getKlhl538mm());
        lqw.eq(bo.getLuoj() != null, WdclzdgmdBsInfo::getLuoj, bo.getLuoj());
        lqw.eq(bo.getMeicjs() != null, WdclzdgmdBsInfo::getMeicjs, bo.getMeicjs());
        lqw.eq(bo.getJiczl() != null, WdclzdgmdBsInfo::getJiczl, bo.getJiczl());
        lqw.eq(bo.getGanmdpjz() != null, WdclzdgmdBsInfo::getGanmdpjz, bo.getGanmdpjz());
        return lqw;
    }

    /**
     * 新增最大干密度\最佳含水率试验检测记录
     */
    @Override
    public Boolean insertByBo(WdclzdgmdBsInfoBo bo) {
        WdclzdgmdBsInfo add = BeanUtil.toBean(bo, WdclzdgmdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 3次
            ArrayList<WdclzdgmdBaDetail> details = new ArrayList<>(3);
            for (long i = 1; i <= 3L; i++) {
                details.add(new WdclzdgmdBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改最大干密度\最佳含水率试验检测记录
     */
    @Override
    @DealHeh
    public Boolean updateByBo(WdclzdgmdBsInfoBo bo) {
        WdclzdgmdBsInfo update = BeanUtil.toBean(bo, WdclzdgmdBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdclzdgmdBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclzdgmdBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除最大干密度\最佳含水率试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
